<?php

/* 
 * Manager/HistoryManager.php
 */
namespace RHX\Model\Manager;

use Zend\Db\Adapter\Adapter;
use RHX\Model\Object\AppHistory;
use RHX\Model\Table\AppHistoryTable;
use RHX\Model\Object\XEnvironment;

class HistoryManager{
    protected $adapter;
    protected $historyTable = NULL;


    /**
     * constructeur
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    public function __construct(Adapter $adapter){
        $this->adapter = $adapter;
    }
    
    /**
     * Adapteur de base de données
     * 
     * @return Adapter
     */
    public function getAdapter(){
        return $this->adapter;
    }
    
    /**
     * Table de base de données
     * 
     * @return AppHistoryTable
     */
    public function getTable(){
        if(is_null($this->historyTable)){
            $this->historyTable = new AppHistoryTable($this->getAdapter());
        }
        return $this->historyTable;
    }
    
    /**
     * Ajout dans l'historique
     * 
     * @param string $libelle
     * @param int $user
     * @return int
     */
    public function push($libelle, $user = 0){
        $history = new AppHistory();
        $history->user = $user;
        $history->libelle = $libelle;
        $history->machine = XEnvironment::hostName() .'(' .XEnvironment::hostIpv4() .')';
        return $this->getTable()
                ->save($history);
    }
}
